/*
 * Author: vyouzhi <vyouzhi@163.com>
 * http://www.xda.cn
 *
 * File: BuildXml.c
 * Create Date: 2011-06-30 13:43:40
 *
 */
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "sdb_struct.h"
#include "mysql_lib.h"
#include "xml_lib.h"
#include "BuildXml.h" 
#include "memcache_lib.h"
#include "conf_lib.h"
#include "link.h"
#include "convey_lib.h"
#include "record_lib.h"

void FreeData(Sdb *sdb){
	Sdb_destroy(sdb);
}

/*
recode 
1 tell all client will update

*/


void del_mem(const char *key){
	del_memcache(key);
}

char *select_memcache(char *key){
	char *membuf = NULL;
	membuf = get_memcache(key);
	return membuf;
}

char *select_mysql(char *sql, int type){
	char *dosql = sql;
	char *buff_xml;
	Sdb *sdb;
	
	if( -1 == connect_sql()) return NULL;
	sdb = get_mysql(dosql, type);

	if(sdb == NULL){
		printf("data is null\n");
		return NULL;
	}else{
		buff_xml = set_xml(sdb);
		FreeData(sdb);
	}

	return buff_xml;
}

char *getData(recv_xml *rxml){
	char *val, *key;
	int i = 0, extime = 0;
	recv_xml *gdxml = rxml;

	val = NULL;
	key = gdxml->key;
	extime = gdxml->extime;
	if(key != NULL && 0 != strcmp(key, "null") && extime > 0){
		val = select_memcache(gdxml->key);
		i = 1;
	}
	if(val == NULL){
		val = select_mysql(gdxml->sub, gdxml->type);
	}else{
		printf("mem key is:%s \n",key);
		return val;
	}
	if(i == 1 && val != NULL){
		set_memcache(val, gdxml->key, extime, strlen(val));
	}

	return val;
}

char *getid(long id){
	char idxml[1024]; 
	char *sd;

	sprintf(idxml, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><data><Row0><id>%ld</id></Row0></data>", id);
	sd = strdup(idxml);

	return sd;
}

long setData(const char *sub){
	long lastid;

	if (-1 == connect_sql()) return(-1);
	lastid = set_mysql(sub);
	if(-1 == lastid) return (-1);
	doconvey(lastid);
	dorecord(sub);
	return lastid;	
}

int insert_log(const char *sub, long id, int logname){
	char *isql, *sql;
	int len = strlen(sub)+1024;
	int i = 0;

	if (-1 == connect_sql() )return (-1);
	sql = escape_mysql(sub);
	isql = (char *)calloc(len, sizeof(char));
	sprintf(isql, "INSERT INTO `sql_log`.`sql_log_%d` (`id`, `sql`)VALUES (%ld, '%s');", logname, id, sql);	
	
	i = query_mysql(sub);

	if(i != 0) {
		return (-1);
	}else{
		i = query_mysql(isql);
		if (-1 == i){
			
			return (-1);
		}
	}
	commit_mysql();

	close_mysql();
	free(isql);
	free(sql);
	return i;
}

int PaserXml(cliInfo *cli, char **senddata){
	recv_xml *rxml;
	cliInfo *cli_p = cli;
	int recode=0;
	long lastid;
	char *sql=NULL;
	
	char *data = cli_p->data;
	int dlen = cli_p->totallen;
	
	rxml = get_xml(data, dlen);

	if(rxml != NULL && rxml->rw != NULL){
		if(rxml->sub != NULL && !strcmp(rxml->rw, "w") && strlen(rxml->sub) > 0){
			sql = strdup(rxml->sub);
			lastid = setData(sql);
				
			if(lastid > 0 && rxml->returnid == 1) *senddata = getid(lastid);
			else *senddata = NULL;
			free(sql);	
			recode = 0;
		}else if(rxml->sub != NULL && !strcmp(rxml->rw, "r") && strlen(rxml->sub) > 0){	
			*senddata = getData(rxml);
		}else if(!strcmp(rxml->rw, "u")){
			printf("recv client---\n");
			getupdate(rxml->lastid);
			printf("here---\n");
			recode = 1;
			*senddata = NULL;
		}else if(!strcmp(rxml->rw, "g")){
			printf("g server---\n");
			updateing(rxml->lastid, rxml->host, rxml->port);
			*senddata = NULL;				
		}else if(!strcmp(rxml->rw, "i")){
			printf("insert log and insert table\n");
			insert_log(rxml->sub, rxml->id, rxml->logname);
			recode = 0;
		}else if(rxml->action != NULL && !strcmp(rxml->rw, "m") && !strcmp(rxml->action, "del")){
			del_mem(rxml->key);
		}


		free(rxml->rw);
		free(rxml->sub);
		free(rxml->key);
		free(rxml->host);
		free(rxml->action);
		free(rxml);	

	}else *senddata = NULL;
	
    return recode;
	
}
/* vim: set ts=4 sw=4: */

